$(function () {
    var dateRangePicker = $('#daterangepicker'), searchBtn = $("#searchAuditRecord");
    var initDateRangePicker = function () {
        var rangePast = {
            '今天': [moment(), moment()],
            '昨天': [moment().subtract(1,'days'), moment().subtract(1,'days')],
            '最近7天': [moment().subtract(6,'days'), moment()],
            '最近30天': [moment().subtract(29,'days'), moment()],
            '本月': [moment().startOf('month'), moment().endOf('month')],
            '上个月': [moment().subtract(1,'month').startOf('month'), moment().subtract(1,'month').endOf('month')]
        };
        var rangeFuture = {
            '今天': [moment(), moment()],
            '未来7天': [moment(), moment().add(6,'days')],
            '未来30天': [moment(), moment().add(29,'days')],
            '未来60天': [moment(), moment().add(59,'days')],
            '本月': [moment().startOf('month'), moment().endOf('month')],
            '下个月': [moment().add(1,'month').startOf('month'), moment().add(1,'month').endOf('month')]
        };
        
        dateRangePicker.daterangepicker({
            ranges: rangePast,
            startDate: moment().subtract(29,'days'),
            endDate: moment(),
            applyClass: "btn-primary btn-block",
            cancelClass: "btn-block",
            format: "YYYY-MM-DD",
            locale: {
                applyLabel: '确定',
                clearLabel: "取消",
                fromLabel: '开始时间',
                toLabel: '结束时间',
                weekLabel: '周',
                customRangeLabel: '选择范围',
                daysOfWeek: "日_一_二_三_四_五_六".split("_"),
                monthNames: "一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月".split("_"),
                firstDay: 0
            },
            opens: "left"
        });

        var formatDate = function (date) {
            return $.format.date(new Date(date), "yyyy/MM/dd");
        };
        // 根据状态设定初始日期
        var defaultDate = formatDate(moment().subtract(29,'days')) + ' - ' + formatDate(moment());
        dateRangePicker.val(defaultDate);
        $(".daterangepicker .ranges ul li:last").hide();
        $(".daterangepicker .ranges .daterangepicker_start_input").remove();
        $(".daterangepicker .ranges .daterangepicker_end_input").remove();
        $(".daterangepicker .ranges .cancelBtn").remove();
    };
    initDateRangePicker();
    /**
     * 初始化曲线审批记录列表
     */
    chinese.sSearch = "用户名搜索";
    var table = $('#withdrawsAuditRecordList').DataTable({
        "processing": true,
        "serverSide": true,
        "sort": false,
        "search": false,
        "ajax": {
            "url": "fund/getWithdrawAuditRecord",
            "data": function (d) {
                var dateRange = dateRangePicker.val().split(" - ");
                    d.startTime = new Date(dateRange[0]);
                    var endTime = moment(dateRange[1], "YYYY/MM/DD").endOf('day');
                    d.endTime = new Date(endTime);
                    d.loginName = $("#loginName").val();
            },
            "type": "POST"
        },
        "aoColumnDefs": [{
                "targets": 0, //订单号
                "mRender": function (data, type, row) {
                    return row.orderId;
                }
            }, {
                "targets": 1, //提现用户账号
                "mRender": function (data, type, row) {
                    return '<a href="user/index/' + row.userId + '" target="_blank">' + row.loginName + '</a>';
                }
            },{
                "targets": 2, //提现用户
                "mRender": function (data, type, row) {
                    return '<a href="user/index/' + row.userId + '" target="_blank">' + row.userName + '</a>';
                }
            }, {
                "targets": 3, //提现金额
                "mRender": function (data, type, row) {
                    return $.number(row.amount, 2);
                }
            },{
                "targets": 4, //提现时间
                "mRender": function (data, type, row) {
                    return $.format.date(new Date(row.withdrawsTime), "yyyy/MM/dd HH:mm");
                }
            }, {
                "targets": 5, //状态
                "mRender": function (data, type, row) {
                    return sysFundStatus[row.status];
                }
            }, {
                "targets": 6, //审批人
                "mRender": function (data, type, row) {
                    return row.auditName;
                }
            }, {
                "targets": 7, //审批时间
                "mRender": function (data, type, row) {
                    return $.format.date(new Date(row.auditTime), "yyyy/MM/dd HH:mm");
                }
            }],
        "order": [6, 'desc'],
        "oLanguage": chinese,
        "iDisplayLength": 20,
        "aLengthMenu": [
            [5, 10, 20, 50, 100, -1],
            [5, 10, 20, 50, 100, "全部"]
        ],
        "sDom": '<"dt-panelmenu clearfix"l<"pull-right"T>r>t<"dt-panelfooter clearfix"ip>',
        "oTableTools": {
            "aButtons": [{
                "sExtends": "copy",
                "sButtonText": "复制",
                "fnComplete": function (nButton, oConfig, oFlash, sFlash) {
                    alert('列表内容已复制到剪贴板');
                }
            },{
                    "sExtends":    "text",
                    "sButtonText": "导出为Excel",
                    "fnClick": function ( nButton, oConfig, oFlash ) {
                        var dateRange = dateRangePicker.val().split(" - ");
                        var startTime = dateRange[0] + " 00:00:00";
                        var endTime = dateRange[1] + " 24:00:00";
                        var loginName = $("#loginName").val();
                        var url = "fund/exportAuditRecord?startTime="+startTime+"&endTime="+endTime+"&loginName="+loginName;
                        window.open(url);
//                        $.post("fund/exportAuditRecord", {startTime: startTime, endTime: endTime, loginName: loginName},
//                            function(res){
//                            
//                            
//                        
//                        });
                    }
                }
            ],
            "sSwfPath": root_path + "/js/plugins/datatables/extensions/TableTools/swf/copy_csv_xls_pdf.swf"
        },
        "fnDrawCallback": function () {
            sys.btnStop(searchBtn);
        }
    });
    
    searchBtn.on('click', function () {
        sys.btnSpin(searchBtn);
        table.ajax.reload();
    });
    
    
    
});
